'''
陈超依 19377189
现代程序设计第十次作业
'''
import abc
import math
from re import A

import imageio
import jieba
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy
from PIL.Image import Image
from wordcloud import WordCloud as W


class PlotterMetaClass(metaclass=abc.ABCMeta):
    @abc.abstractclassmethod
    def plot(data, *args, **kwargs):
        pass


class PointPlotter(PlotterMetaClass):
    def plot(data : list, *args, **kwargs):
        X=[]
        Y=[]
        for i in data:
            X.append(i[0])
            Y.append(i[1])
        plt.plot(X, Y, *args, **kwargs)
        plt.show()


class ArrayPlotter(PlotterMetaClass):
    def plot(data : list, *args, **kwargs):
        if (len(data) == 2):
            X = data[0]
            Y = data[1]
            plt.plot(X, Y, *args, **kwargs)
        if (len(data) == 3):
            X = data[0]
            Y = data[1]
            Z = data[2]
            fig = plt.figure()
            ax = fig.gca(projection='3d')
            ax.plot(X, Y, Z, *args, **kwargs)
            ax.legend()
            plt.show()


class TextPlotter(PlotterMetaClass):
    def plot(data : str, *args, **kwargs):
        Words = jieba.lcut(data)
        #统计词频
        Count = { }
        StopWords = open("E:/Py_Programs/week2/StopWords_list.txt",mode='r',encoding='utf-8').read()
        StopWords = StopWords.split("\n")
        for word in Words:
            if word not in StopWords:
                if word in Count:
                    Count[word] = Count[word]+1
                else:
                    Count[word] = 1
        #筛选高频词
        frequent_words = []
        for word in Count:
            if (Count[word] > 1):
                frequent_words.append(word)
        #绘词云图
        WordS = ' '.join(frequent_words)
        WC = W(*args, **kwargs).generate(WordS)
        plt.imshow(WC)
        plt.axis('off')
        plt.show()
        

class ImagePlotter(PlotterMetaClass):
    def plot(data, *args, **kwargs):
        if (type(data) == str):
            img = mpimg.imread(data)
            img.shape
            plt.imshow(img, *args, **kwargs)
            plt.axis('off')
            plt.show()
        if (type(data) == list):
            n = math.ceil(math.sqrt(len(data)))
            m = math.ceil(len(data)/n)
            for i in range(len(data)):
                plt.subplot(m,n, i+1)
                img = mpimg.imread(data[i])
                img.shape
                plt.axis('off')
                plt.imshow(img, *args, **kwargs)
            plt.show()


class GifPlotter(PlotterMetaClass):
    def plot(data : list, *args, **kwargs):
        imgs = []
        for img in data:
            imgs.append(imageio.imread(img))
        imageio.mimwrite('./test.gif',imgs, 'GIF', *args, **kwargs)
        plt.axis('off')
        plt.imshow(mpimg.imread('./test.gif'))
        plt.show()


def main():
    #测试PointPlotter
    '''test_data = [(0,1), (1,2), (2,4), (3,6), (4,8)]
    PointPlotter.plot(test_data,'r--*')'''
    #测试ArrayPlotter
    '''test_data = [[1,2,3],[4,5,6],[7,8,9]]
    ArrayPlotter.plot(test_data)'''
    #测试TextPlotter
    '''test_data = '在使用python时，我们经常会用到许多工具库，它们提供了较为方便的函数调用。但\
是仍然会有一些情况，例如数据类型或格式不符合函数要求，参数存在差异等，使得\
调用前需要对数据进行额外处理。本次作业要求基于matplotlib，wordcloud，PIL,\
imageio等绘图库的绘制函数，设计并实现适配器抽象类和不同的适配类，以实现不同\
类型数据的多样化可视。具体要求如下'
    TextPlotter.plot(test_data, font_path=r'C:/Windows/Fonts/FZSTK.TTF',
    background_color='white', width=1000, height=800, collocations=False)'''
    #测试ImagePlotter
    '''test_data = ['E:/Py_Programs/week9/originalPics/2002/07/19/big/img_18.jpg',
        'E:/Py_Programs/week9/originalPics/2002/07/19/big/img_517.jpg',
        'E:/Py_Programs/week9/originalPics/2002/07/19/big/img_822.jpg',
        'E:/Py_Programs/week9/originalPics/2002/07/19/big/img_828.jpg']
    ImagePlotter.plot(test_data)'''
    #测试GifPlotter
    test_data = ['E:/Py_Programs/week9/originalPics/2002/07/19/big/img_18.jpg',
        'E:/Py_Programs/week9/originalPics/2002/07/19/big/img_517.jpg',
        'E:/Py_Programs/week9/originalPics/2002/07/19/big/img_822.jpg',
        'E:/Py_Programs/week9/originalPics/2002/07/19/big/img_828.jpg']
    GifPlotter.plot(test_data, duration=0.5)


if __name__=='__main__':
	main()